home *** CD-ROM | disk | FTP | other *** search
- The text below is from The C Gazette, Summer 1988, Volume 3
- No. 1, and is the magazine text for PCFILERD.C.
-
- ----------------------------------------------------------------------------
- PC-FILE and it's successor PC-FILE+ are the most widely distributed data-
- base managers for the PC. Their success is due to their ease of use and their
- availability as shareware. This article discusses the file formats and access
- to the data and index records using C.
-
- PC-FILE+ provides programmers with an inexpensive front-end to database
- applications. The programs that handle data-entry and record management are
- commonly available as shareware. They are ideal for the management of small
- databases. Few packages are as easy to install and use, and none offers so
- much functionality at such a low price. The complete system with a bound
- manual and technical support is available for $69.95 from Buttonware (ad-
- dress at end of article).
- PC-FILE manages data as a flat file that is a collection of fixed-length
- sequential records. A separate index file permits random access to individual
- data records.
- The index file contains one record for each data record. Every field in
- the data record has a corresponding two-byte index in the index record, such
- that every field of every data record is accessible directly. The index field
- contains the first two bytes of the data field, such that a name field cont-
- aining "ERIC", for example, would have an index entry of "ER". The index
- record contains two bytes for each data field plus a two-byte unsigned integer
- at the end which holds the record number of the data. This number is limited
- to the range of an unsigned int, 65536, and predictably, this is the upper
- limit on the number of records supported in one database. (Early versions used
- a signed integer and were limited thereby to half the current capacity.)
- To find a data record based on the record number, the following formula
- is to calculate the record's offset into the data file:
- (record number - 1) * length of data
- The record number should be decremented because the first record (number
- 1) starts at an offset of zero bytes into the file. The length of the data
- record is the sum of all the data fields plus one byte. This byte is a
- carriage return (0x0d) which PC-FILE appends to each data record.
- To access a database, we need to know the number of fields in each record
- (to know the length of the index record) and how many bytes of data each
- record holds (to know the length of the data records). This information is
- available in a separate header file maintained by PC-FILE.
- Every database contains at least three files: filename.inx, filename.dta,
- and filename.hdr. These are the index, data, and header files respectively.
- The header file is a straight ASCII file which lists each data field: it's
- name, size, and placement in the record. The keys in the index file follow the
- same order as the data record layout.
- Access to a data file is done as follows. Suppose a mailing list file
- were being queried for all persons living in Dubuque. The index file would be
- read one record at a time and the city key would be checked against the first
- two bytes of the search key, namely "DU". When a match occurs, the data record
- is read (based on calculation the offset into the data file) and the actual
- city name is verified. This check avoids denizens of DUluth from being
- accepted by the query.
- A sequential read of the file requires reading each index record and look-
- ing up the data record. The accompanying program, PCFILERD.C, demonstrates the
- technique of a sequential read through a file.
- Two special cases need to be accounted for. Deleted records are flagged
- by the placement of a forward slash in the first byte of the index record.
- Such records should simply be skipped. End-of-file is indicated by a backslash
- in the first byte. This should always be checked for and considered a true EOF
- condition. PC-FILE maintains one record at file's end with this flag; hence a
- sequential read will never attempt to go beyond the end-of-file. The function
- get_record() illustrates the handling of these special cases.
- ( Typist's note: The accompanying program displays my database. The func-
- tion print_line() as it appears in the magazine article follows below. If you
- have trouble printing your database, try replacing the print_line() in the
- accompanying program PCFILERD.C with this version: )
-
-
- void print_line() /* Just a sample application: print the first */
- { /* forty characters of the data record */
-
- char line [40];
-
- strncpy (line, Data_buffer, 40);
- if (strlen (line) > 39) /* Remember, strncpy() may not end */
- line[39] = '\0'; /* the string with a null. */
- printf ("%s\n", line);
- }
-
- This application is simply for demonstration, since all forms of data
- manipulation are of course possible.
- The information in this article relies in no small part on material pre-
- sented in the manual to PC-FILE+. (Typist's note: I used this program with
- PC-FILE III Ver.4 and it works fine.) This book should be bought by anyone
- who wishes to use PC-FILE+ to it's full effect. An appendix contains some
- sample C source code which demonstrates file access. However, it would be kind
- to say that the author's native language is not C. The coding techniques
- should not be imitated at all, and the routines presented here should be used
- instead.
- PC-FILE+ is sold by Buttonware, P.O. Box 5786, Bellevue, WA 98006.
- (800) JBUTTON. Not surprisingly, PC-FILE and PC-FILE+ are trademarks of
- Buttonware.
-
-